package model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import play.Logger;
import play.db.jpa.JPA;

@Entity
@Table(name = "ORGANIZATION")
@NamedQueries({
	@NamedQuery(name="Organization.findBy_code", query="SELECT o FROM Organization o WHERE o.code = :code")
})
public class Organization implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private long id;

	private String stationlvl;

	private String city;

	@Column(nullable = false, length = 20)
	private String code;

	@Column(precision = 5, nullable = true)
	private Integer connectcosttime;

	@Column(nullable = false, precision = 10)
	private long createby;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false)
	private Date createtime;

	@Column(nullable = false, precision = 1)
	private boolean isactive;

	private String isonline;

	@Column(nullable = false, length = 100)
	private String name;

	@Column(nullable = true, precision = 10)
	private Long parentid;

	@Column(length = 100)
	private String serversaddress;

	private String address;

	@Column(nullable = false, length = 10)
	private String type;

	private String linkman;

	private String phoneno;

	private String trafficroute;

	@Column(precision = 10, nullable = false)
	private long updateby;

	@Column(nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date updatetime;

	@OneToMany(mappedBy = "organization")
	private List<Department> departments;

	@Transient
	private Boolean isSelect;

	@Column(length = 100)
	private String syncserversaddress;

	public String extendinfo;
	
	public Organization() {
	}

	public long getId() {
		return this.id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCode() {
		return this.code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Integer getConnectcosttime() {
		return this.connectcosttime;
	}

	public void setConnectcosttime(Integer connectcosttime) {
		this.connectcosttime = connectcosttime;
	}

	public long getCreateby() {
		return this.createby;
	}

	public void setCreateby(long createby) {
		this.createby = createby;
	}

	public Date getCreatetime() {
		return this.createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	public boolean getIsactive() {
		return this.isactive;
	}

	public void setIsactive(boolean isactive) {
		this.isactive = isactive;
	}

	public String getIsonline() {
		return isonline;
	}

	public void setIsonline(String isonline) {
		this.isonline = isonline;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getParentid() {
		return this.parentid;
	}

	public void setParentid(Long parentid) {
		this.parentid = parentid;
	}

	public String getServersaddress() {
		return this.serversaddress;
	}

	public void setServersaddress(String serversaddress) {
		this.serversaddress = serversaddress;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getUpdateby() {
		return this.updateby;
	}

	public void setUpdateby(long updateby) {
		this.updateby = updateby;
	}

	public Date getUpdatetime() {
		return this.updatetime;
	}

	public void setUpdatetime(Date updatetime) {
		this.updatetime = updatetime;
	}

	public List<Department> getDepartments() {
		return this.departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public String getServersaddressIp() {
		if (null == getServersaddress())
			return null;
		int index = getServersaddress().indexOf(":");
		if (index < 0) {
			return getServersaddress();
		} else {
			return getServersaddress().substring(0, index);
		}
	}

	public String getServersaddressPort() {
		if (null == getServersaddress())
			return null;
		int index = getServersaddress().indexOf(":");
		if (index < 0) {
			return "3700";
		} else {
			return getServersaddress().substring(index + 1);
		}
	}

	public Boolean getIsSelect() {
		return isSelect;
	}

	public void setIsSelect(Boolean isSelect) {
		this.isSelect = isSelect;
	}

	public String getStationlvl() {
		return stationlvl;
	}

	public void setStationlvl(String stationlvl) {
		this.stationlvl = stationlvl;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getLinkman() {
		return linkman;
	}

	public void setLinkman(String linkman) {
		this.linkman = linkman;
	}

	public String getPhoneno() {
		return phoneno;
	}

	public void setPhoneno(String phoneno) {
		this.phoneno = phoneno;
	}

	public String getTrafficroute() {
		return trafficroute;
	}

	public void setTrafficroute(String trafficroute) {
		this.trafficroute = trafficroute;
	}

	public String getSyncserversaddress() {
		return syncserversaddress;
	}

	public void setSyncserversaddress(String syncserversaddress) {
		this.syncserversaddress = syncserversaddress;
	}
	
	public static Organization findBy_code(String code,boolean flag){
		String currrntDB = JPA.getCurrentConfigName();
		Organization org = null;
		try{
			JPA.setCurrentConfigName("play");
			List<Organization> list = JPA.em().createNamedQuery("Organization.findBy_code", Organization.class)
					.setParameter("code", code).getResultList();
			if(null != list && list.size() >0){
				org = list.get(0);
			}
			return org;
		}catch(Exception e){
			if(flag){
				Logger.log4j.error("查询机构信息异常code:"+code, e);
			}
			return org;
		}finally{
			JPA.setCurrentConfigName(currrntDB);
		}
	}

}